<?php
namespace api\xcx\controller;
use think\Cache;
use think\Loader;
use think\Db;
use think\Request;
use think\Controller;
use think\config;
class Shoporder extends Base
/**
 *  Shoporder.php 上门订单控制器
 *
 * @copyright			(C) 2014-2018 ShengTeng
 * @lastmodify			2018-05-21
 */

{
/**
     * @api {POST} Shoporder/shopOrderPayWx 门店订单支付(微信)返回订单号,生成金额
     * @apiVersion 1.0.0
     * @apiGroup 门店订单
     *
     * @apiParam {String} orderNumber  订单号
     * @apiParam {String} token  员工token
     * @apiParam {String} goods_id  项目id,逗号隔开
     * @apiParam {String} goodsPrice  项目金额,逗号隔开
     * @apiParam {String} shopId 店铺id
     * @apiParam {Int} purePrice  纯色美甲金额
     *
     * @apiSuccess {Int} code 返回码
     * @apiSuccess {String} message  提示信息
     * @apiSuccess {Object} data  返回数据
     *
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "code":0,
     *          "message":"",
     *          "data":{
     *           "orderNumber": 订单号
     *          }
     *      }
     */
    public function shopOrderPayWx(Request $request){
        $params=$request->param();
         // token
         $token = $this->checkParam($params['token'],'token',true);
        // 订单号
        $orderNumber = $this->checkParam($params['orderNumber'],'orderNumber',true);
        // 项目id,
        $goods_id = $this->checkParam($params['goods_id'],'goods_id',true);
        // 项目价格,
        $goodsPrice = $this->checkParam($params['goodsPrice'],'goodsPrice',true);
         // 店铺id,
        $shopId = $this->checkParam($params['shopId'],'shopId',true);
        $purePrice = $this->checkParam($params['purePrice'],'purePrice',true);
        // 查看订单是否存在
        $isOrder = $this->selOrder($orderNumber);
        // 查看员工是否存在
        $staffId = $this->selStaff($token);
        // 修改订单
        $oldPrice = 0;//原价
        $vipPrice = 0;//vip价
        $goodsPrices = explode(',',$goodsPrice);
        $goodsId = explode(',',$goods_id);
        // 原价
        for($i=0;$i<count($goodsPrices);$i++){
            $oldPrice+=$goodsPrices[$i];
        }
        // 根据项目id获取项目折扣和总项目id
        $goiddscount = array();
        $agoid = array();
        // vip价
        $shopConfig = Model('ShopConfig');
        for($i=0;$i<count($goodsId);$i++){
            // 获取每个项目的信息
            $goodsInfo = $shopConfig->selById($goodsId[$i],$shopId);
            if(!$goodsInfo){
                return json(returnAjax('','该店铺无此项目','1'));
              }
            $discount = $goodsInfo['discount'];//折扣
            $serverGoods = $goodsInfo['commondityId'];
            array_push($goiddscount,$discount);
            array_push($agoid,$serverGoods);
            // if($serverGoods == 1){
            //     $vipPrice+=0;
            // }else if($serverGoods == 2){
            //     // 获取纯色价格
            //     $vipPrice+=($goodsPrices[$i]-$purePrice)*$discount;
            // }else{
            //     $vipPrice+=$goodsPrices[$i]*$discount;
            // }
            $vipPrice+=$goodsPrices[$i]*$discount;
        }
        $data['commondityDiscount'] = implode(',',$goiddscount);//项目折扣
        $data['acommondityId'] = implode(',',$agoid);//总项目id
        $data['staffId'] = $staffId['userId'];//员工id
        $data['shopId'] = $shopId;//店铺id
        $data['commodityId'] = $goods_id;//项目id
        $data['commodityPrice'] = $goodsPrice;//项目价格
        $data['amount'] = $oldPrice;//原价
        $data['vipAmount'] = $vipPrice;//vip价
        $order = Model('Order');
        // 门店订单修改
        $upd = $order->staffSQrcode($data,$orderNumber);
        if($upd){
            $res['orderNumber'] = $orderNumber;
            return json(returnAjax($res));
        }else{
            return json(returnAjax('','系统错误','-1'));
        }
    }
}
